package com.healthy.dao;

import com.healthy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class UserDao {

    @Autowired
    JdbcTemplate template;

    public boolean saveUser(User user) {
        return template.update("insert into t_user (name, account, password) values (?, ?, ?)",
                user.getName(), user.getAccount(), user.getPassword()) > 0;
    }

    public boolean setTokenToUser(String token, Integer id) {
        return template.update("update t_user set token = ? where id = ?", token, id) > 0;
    }

    public User getUserByToken(String token) {
        try {
            return template.queryForObject("select * from t_user where token = ?",
                    new BeanPropertyRowMapper<>(User.class),
                    token);
        } catch (Exception e) {
            return null;
        }
    }
}
